<?php

namespace app\common\model\Hotel;

use think\Model;


class Coupon extends Model
{
    //
    protected $name = "hotel_coupon";

    // //指定一个自动设置的时间字段
    // //开启自动写入
    // protected $autoWriteTimestamp = true;

    // //设置字段的名字
    // protected $createTime = "createtime"; //插入的时候设置的字段名

    // //禁止 写入的时间字段
    // protected $updateTime = false;


    protected $append = [
        'createtime_text',
        'status_text',
        'endtime_text',
        'thumb_text',
        'rate_text'
    ];


    public function getCreateTimeTextAttr($value,$data){
        $createtime = isset($data['createtime']) ? trim($data['createtime']) : '';
        if(empty($createtime)){
            return '';
        }
        return date('Y-m-d',$createtime);
    }

    public function getStatusTextAttr($value,$data){
        if(!isset($data['id'])){
            return NULL;
        }else{
            $status = isset($data['status']) ? trim($data['status']) : '';

            $status_list = [0 => '活动未开始', 1 => '活动进行中', 2 => '活动结束'];

            if($status >= 0){
                return $status_list[$status];
            }
        }
        
    }

    public function getEndTimeTextAttr($value,$data){
        $endtime = isset($data['endtime']) ? trim($data['endtime']) : '';
        if(empty($endtime)){
            return '';
        }
        return date('Y-m-d',$endtime);
    }

    public function getThumbTextAttr($value, $data){
        //获取到cdn的地址
        $cdnurl = config('site.cdnurl') ? config('site.cdnurl') : '';
        $cdnurl = trim($cdnurl, '/');
        
        // 多张图字符串结构
        $thumb = isset($data['thumb']) ? $data['thumb'] : '';

        if(empty($thumb))
        {
            $thumb = "/assets/img/hotel1.jpg";
        }else
        {
            $thumb = explode(',', $thumb);
            if(!empty($thumb))
            {
                $pic = '';

                foreach($thumb as $item)
                {
                    if(is_file(".".$item))
                    {
                        $pic = $item;
                        break;
                    }
                }

                if(empty($pic))
                {
                    $pic = "/assets/img/coupon.jpg";
                }

                $thumb = $pic;
            }
        }

        return $cdnurl.$thumb;
    }

    public function getRateTextAttr($value,$data){
        $rate = isset($data['rate']) ? trim($data['rate']) : '';
        if(empty($rate)){
            return '';
        }
        $rate = $rate * 10;
        return $rate."折";
    }

}
